package org.davidliebman.android.awesomecube;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.Toast;

public class CubeOptionsActivity extends Activity {

	public static final String AWESOME_NAME = new String("org.awesomecube");
	public static final String SAVED_PLAY_ANIMATION = new String("saved_play_animation");
	public static final String SAVED_SCRAMBLE_CUBE = new String("saved_scremble_cube");
	public static final String SAVED_BREADCRUMBS = new String("saved_breadcrumbs");
	public static final String SAVED_CLEAR_BREADCRUMBS = new String("saved_clear_breadcrumbs");
	public static final String SAVED_DISPLAY_ANGLE = new String("saved_display_angle");
	public static final String SAVED_USE_MIRROR = new String("saved_use_mirror");
	
	public boolean mAnimate = true;
	public boolean mEraseMovelist = false;
	public boolean mDisplayAtAngle = false;
	public boolean mUseMirror = false;
	public int mSaveState = 0;
	public static final int STATE_SCRAMBLE = 1;
	public static final int STATE_SAVED = 2;
	public static final int STATE_SOLVED = 3;
	
	
	public Button gobutton;
	public CheckBox checkbox_mirror;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.options);
	
        SharedPreferences preferences = getSharedPreferences(AWESOME_NAME, MODE_PRIVATE);
        mAnimate = preferences.getBoolean(SAVED_PLAY_ANIMATION, false);
        mSaveState = preferences.getInt(SAVED_SCRAMBLE_CUBE, STATE_SOLVED);
        mDisplayAtAngle = preferences.getBoolean(SAVED_DISPLAY_ANGLE, false);
        mUseMirror = preferences.getBoolean(SAVED_USE_MIRROR, true);
        
        final CheckBox checkbox_turning = (CheckBox) findViewById(R.id.check_animation);
        checkbox_turning.setChecked(mAnimate);
        checkbox_turning.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

            	if (((CheckBox) v).isChecked()) {
                    mAnimate = true;
                } else {
                    mAnimate = false;
                }
            }
        });
        
        final OnClickListener radio_scramble = new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
                RadioButton rb = (RadioButton) v;
                //Toast.makeText(Options.this, rb.getText(), Toast.LENGTH_SHORT).show();
                
                if(rb.getId() == R.id.radio_scramble) {
                	mSaveState = STATE_SCRAMBLE;
                	gobutton.setText("Scrambled Cube");

                }
                if(rb.getId() == R.id.radio_solved) {
                	mSaveState = STATE_SOLVED;
                	gobutton.setText("Solved Cube");

                }
                if(rb.getId() == R.id.radio_saved) {
                	mSaveState = STATE_SAVED;
                	gobutton.setText("Last Cube");
                }
                
            }
        };
        
        //placed out of sequence so that 'chackbox_change_angle' can disable 'checkbox_mirror'
        checkbox_mirror = (CheckBox) findViewById(R.id.check_mirror);        
        checkbox_mirror.setChecked(mUseMirror);
        checkbox_mirror.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

            	if (((CheckBox) v).isChecked()) {
            		mUseMirror = true;
            	} else {
                    mUseMirror = false;
                }
            }
        });
        
        final CheckBox checkbox_change_angle = (CheckBox) findViewById(R.id.check_angle);
        checkbox_change_angle.setChecked(mDisplayAtAngle);
        checkbox_mirror.setEnabled(! mDisplayAtAngle);
        checkbox_change_angle.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

            	if (((CheckBox) v).isChecked()) {
            		mDisplayAtAngle = true;
            	} else {
                    mDisplayAtAngle = false;
                }
            	checkbox_mirror.setEnabled(! mDisplayAtAngle);
            }
        });
        
      
        
        final CheckBox checkbox_erase_movelist = (CheckBox) findViewById(R.id.check_movelist);
        checkbox_erase_movelist.setChecked(false);
        checkbox_erase_movelist.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

            	if (((CheckBox) v).isChecked()) {
                    mEraseMovelist = true;
                } else {
                    mEraseMovelist = false;
                }
            }
        });
     
		gobutton = (Button) findViewById(R.id.button_game);
	    gobutton.setOnClickListener(new OnClickListener() {
	        public void onClick(View v) {
	        	if(mSaveState == STATE_SOLVED) {
	        		Intent StartGameIntent = new Intent(CubeOptionsActivity.this, GlCubeActivity.class);
	        		startActivity(StartGameIntent);
	        	}
	        	else if(mSaveState == STATE_SAVED) {

	        		Intent StartGameIntent = new Intent(CubeOptionsActivity.this, GlCubeActivity.class);
		        	startActivity(StartGameIntent);

	        	}
	        	else if (mSaveState == STATE_SCRAMBLE) {
	        		// scramble cube
	        		Intent StartGameIntent = new Intent(CubeOptionsActivity.this, GlCubeActivity.class);
	        		startActivity(StartGameIntent);
	        	}
	        		
	        }
	    });
	    
	    final RadioButton radio_scramble_scramble = (RadioButton) findViewById(R.id.radio_scramble);
        if (mSaveState == STATE_SCRAMBLE ) {
        	radio_scramble_scramble.setChecked(true);
        	gobutton.setText("Scrambled Cube");
        }
        final RadioButton radio_scramble_solved = (RadioButton) findViewById(R.id.radio_solved);
        if (mSaveState == STATE_SOLVED) {
        	radio_scramble_solved.setChecked(true);
        	gobutton.setText("Solved Cube");
        }
        final RadioButton radio_scramble_saved = (RadioButton) findViewById(R.id.radio_saved);
        if ( mSaveState == STATE_SAVED) {
        	radio_scramble_saved.setChecked(true);
        	//what if previous saved state??
        	gobutton.setText("Last Cube");
        }
        
        radio_scramble_scramble.setOnClickListener(radio_scramble);
        radio_scramble_solved.setOnClickListener(radio_scramble);
        radio_scramble_saved.setOnClickListener(radio_scramble);
   
	}
	
	@Override
	public void onPause() {
        
		SharedPreferences preferences = getSharedPreferences(AWESOME_NAME, MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putBoolean(SAVED_PLAY_ANIMATION, mAnimate);
        edit.putInt(SAVED_SCRAMBLE_CUBE, mSaveState);
        edit.putBoolean(SAVED_CLEAR_BREADCRUMBS, mEraseMovelist);
        edit.putBoolean(SAVED_DISPLAY_ANGLE, mDisplayAtAngle);
        edit.putBoolean(SAVED_USE_MIRROR, mUseMirror);
        edit.commit();
        
        
		super.onPause();
	}
	
	@Override
	public void onResume() {
		
        SharedPreferences preferences = getSharedPreferences(AWESOME_NAME, MODE_PRIVATE);
        mAnimate = preferences.getBoolean(SAVED_PLAY_ANIMATION, false);
        mSaveState = preferences.getInt(SAVED_SCRAMBLE_CUBE, STATE_SOLVED);
		mDisplayAtAngle = preferences.getBoolean(SAVED_DISPLAY_ANGLE, false);
		mUseMirror = preferences.getBoolean(SAVED_USE_MIRROR, true);
		
        checkbox_mirror.setEnabled(! mDisplayAtAngle);

        super.onResume();
	}
}
